package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.text.TextUtils;

import com.shuqi.application.PasswordProcess;
import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.HttpCommonParamsUtils;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.AccountBindParser;

public class AccountBindModel extends ModelBase implements HttpRequestListener {

    public static final int TYPE_BIND_PHOEN = 1;// 1表示绑定手机号

    public static final int TYPE_BIND_EMAIL = 2;// 2表示绑定邮箱

    public static final int TYPE_BIND_SINA = 3;// 3表示绑定新浪微博

    private HttpRequest mHttpRequest;

    private Context mContext;

    private String mAccount;

    private String mPassword;

    private int mType;// 绑定接口的类型

    private String mVerifyCode;// 验证码，type为手机时传入

    private String mWeiboName;// type为微博时传入

    public AccountBindModel(Context context) {
        this.mContext = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = HttpCommonParamsUtils.getAccountBasicParams();
        
        params.add(new BasicNameValuePair("account", mAccount));
        if (!TextUtils.isEmpty(mPassword)) {
            params.add(new BasicNameValuePair("password", PasswordProcess.getLgPassword(mPassword)));
        }
        params.add(new BasicNameValuePair("type", String.valueOf(mType)));
        if (mType == TYPE_BIND_PHOEN) {
            params.add(new BasicNameValuePair("vcode", mVerifyCode));
        } else if (mType == TYPE_BIND_SINA) {
            params.add(new BasicNameValuePair("name", mWeiboName));
        }
        return params;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getAccountBindUrl();
    }

    @Override
    public void onDestroy() {
        if (mHttpRequest != null) {
            mHttpRequest.abort();
        }
    }

    public void bind(String account, String password, int type, String vcode, String weiboName) {
        mAccount = account;
        mPassword = password;
        mType = type;
        mVerifyCode = vcode;
        mWeiboName = weiboName;
        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        if (type == TYPE_BIND_SINA) {
            mHttpRequest.setDataParseAdapter(new AccountBindParser(account, weiboName));
        } else {
            mHttpRequest.setDataParseAdapter(new AccountBindParser());
        }

        MyTask.runInBackground(mHttpRequest, true);
    }

    @Override
    public void loadData(Object... objs) {}

}
